package com.zsyh.jiujiu.home;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.zyh.toolslib.base.BaseActivity;
import com.example.zyh.toolslib.utils.EmptyUtil;
import com.example.zyh.toolslib.utils.SPUtils;
import com.example.zyh.toolslib.widget.TitleBar;
import com.google.gson.Gson;
import com.lwb.retrofit.RetrofitTask;
import com.zsyh.jiujiu.Adress.SelectAddresFinish;
import com.zsyh.jiujiu.Adress.TestAdressPop;
import com.zsyh.jiujiu.Api;
import com.zsyh.jiujiu.R;
import com.zsyh.jiujiu.bean.SucessBean;
import com.zsyh.jiujiu.dialog.ClallBackData;
import com.zsyh.jiujiu.fragment.orderFragment.AddAdressFragment;
import com.zsyh.jiujiu.net.MyObserver;
import com.zsyh.jiujiu.utils.ToastUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import retrofit2.http.Query;

/**
 * @ProjectName: livvehelp
 * @Package: com.zsyh.lsb.ui
 * @ClassName:
 * @Description: java类作用描述
 * @Author: zsyh
 * @CreateDate: 2020/9/7 11:59
 * @UpdateUser: liangwanbo
 * @UpdateDate: 2020/9/7 11:59
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class AddAdress extends BaseActivity {

    @BindView(R.id.title_bar)
    TitleBar titleBar;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.name)
    EditText name;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.phone)
    EditText phone;
    @BindView(R.id.tv_diqu)
    TextView tvDiqu;
    @BindView(R.id.diqu)
    TextView diqu;
    @BindView(R.id.shengshi)
    RelativeLayout shengshi;
    @BindView(R.id.tv_xiangxi)
    TextView tvXiangxi;
    @BindView(R.id.xiangxi)
    EditText xiangxi;
    @BindView(R.id.checkbox)
    CheckBox checkbox;
    @BindView(R.id.baocun)
    TextView baocun;
    private String provinceCodes, cityCodes, areaCodes;
    private String pname,cname,dname;
    private String ismoren,shoujihao,xingming,dizhi,dizhiid;

    @Override
    protected int initLayout() {
        return R.layout.activitty_adddizhi;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {
        provinceCodes=getIntent().getStringExtra("provinceCodes");
        cityCodes=getIntent().getStringExtra("cityCodes");
        areaCodes=getIntent().getStringExtra("areaCodes");
        pname=getIntent().getStringExtra("pname");
        cname=getIntent().getStringExtra("cname");
        dname=getIntent().getStringExtra("dname");
        ismoren=getIntent().getStringExtra("ismoren");
        shoujihao=getIntent().getStringExtra("shoujihao");
        xingming=getIntent().getStringExtra("xingming");
        dizhi=getIntent().getStringExtra("dizhi");
        dizhiid=getIntent().getStringExtra("dizhiid");
        diqu.setText(pname+cname+dname);
        xiangxi.setText(dizhi);
        if(ismoren.equals("1")){
            checkbox.setChecked(true);
        }else{
            checkbox.setChecked(false);
        }
        phone.setText(shoujihao);
        name.setText(xingming);
    }

    @Override
    protected void initEvent() {
        titleBar.setLeftLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }


    @OnClick({R.id.shengshi, R.id.baocun})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.shengshi:
                TestAdressPop pop = new TestAdressPop(this);
                pop.setSelectAddresFinish(new SelectAddresFinish() {
                    @Override
                    public void finish(String provinceCode, String cityCode, String areaCode, String provincename, String cityname, String areaname) {
                        provinceCodes = provinceCode;
                        cityCodes = cityCode;
                        areaCodes = areaCode;
                        pname=provincename;
                        cname=cityname;
                        dname=areaname;
                        diqu.setText(provincename + cityname + areaname);
                    }
                });
                pop.show(getFragmentManager(), "address");
                break;
            case R.id.baocun:
                if (EmptyUtil.isEmpty(name.getText().toString())) {
                    ToastUtils.showShort(thisActivity, "请输入姓名");
                    return;
                }

                if (EmptyUtil.isEmpty(phone.getText().toString())) {
                    ToastUtils.showShort(thisActivity, "请输入手机号");
                    return;
                } else {
                    if (isPhoneNumber(phone.getText().toString()) == false) {
                        ToastUtils.showShort(thisActivity, "请输入正确的手机号");
                        return;
                    }
                }
                if (EmptyUtil.isEmpty(areaCodes)) {
                    ToastUtils.showShort(thisActivity, "请选择省市县");
                    return;
                }

                if (EmptyUtil.isEmpty(xiangxi.getText().toString())) {
                    ToastUtils.showShort(thisActivity, "请输入详细地址");
                    return;
                }

                if (checkbox.isChecked()) {
                    ismoren = "1";
                } else {
                    ismoren="0";
                }

                if(EmptyUtil.isEmpty(dizhiid)){
                    AddAdress((String) SPUtils.get(thisActivity, "userid", ""),
                            name.getText().toString(),
                            phone.getText().toString(),
                            provinceCodes,
                            cityCodes,
                            areaCodes,
                            pname,
                            cname,
                            dname,
                            xiangxi.getText().toString(),
                            ismoren
                    );
                }else{
                    xiugai(dizhiid,(String) SPUtils.get(thisActivity, "userid", ""),
                            name.getText().toString(),
                            phone.getText().toString(),
                            provinceCodes,
                            cityCodes,
                            areaCodes,
                            pname,
                            cname,
                            dname,
                            xiangxi.getText().toString(),
                            ismoren);
                }


                break;
        }
    }

    /**
     * 添加地址
     */
    public void AddAdress(String userId,
                          String userName,
                          String phone,
                          String pro,
                          String city,
                          String area,
                          String proName,
                          String cityName,
                          String areaName,
                          String desc,
                          String isDefault) {
        RetrofitTask.getInstance()
                .createApi(Api.class)
                .saveAddress(userId,
                        userName,
                        phone,
                        pro,
                        city,
                        area,
                        proName,
                        cityName,
                        areaName,
                        desc,
                        isDefault)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new MyObserver(this, true) {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(String JsonStr) {
                        Log.e("tianjiadizhi", JsonStr);
                        Gson gson=new Gson();
                        SucessBean sucessBean=gson.fromJson(JsonStr,SucessBean.class);
                        if(sucessBean.getCode()==0){
                            thisActivity.finish();
                        }

                    }
                });


    }

    /**
     * 修改地址
     */
    public void xiugai(String id,String userId,
                       String userName,
                       String phone,
                       String pro,
                       String city,
                       String area,
                       String proName,
                       String cityName,
                       String areaName,
                       String desc,
                       String isDefault){

        RetrofitTask.getInstance()
                .createApi(Api.class)
                .updateAddress(id,userId,
                        userName,
                        phone,
                        pro,
                        city,
                        area,
                        proName,
                        cityName,
                        areaName,
                        desc,
                        isDefault)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new MyObserver(this, true) {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(String JsonStr) {
                        Log.e("tianjiadizhi", JsonStr);
                        Gson gson=new Gson();
                        SucessBean sucessBean=gson.fromJson(JsonStr,SucessBean.class);
                        if(sucessBean.getCode()==0){
                            thisActivity.finish();
                        }

                    }
                });

    }

}
